//
//  LXMovieShowViewController.m
//  cat
//
//  Created by imera on 16/1/16.
//  Copyright © 2016年 LX. All rights reserved.
//

#import "LXMovieShowViewController.h"
#import "MoviePlayerViewController.h"
#import "UIImage+Lx.h"
#import "LXNavBarView.h"
#import "UIImageView+WebCache.h"

@interface LXMovieShowViewController () <UIScrollViewDelegate,LXNavBarViewDelegate>
/*** 顶部视图 */
@property (nonatomic, weak) UIView *topView;
/*** 底部视图 */
@property (nonatomic, weak) UIView *bottomView;
/*** 当前选择的btn */
@property (nonatomic, weak) UIButton *currentBtn;
/*** 当前选集 */
@property (nonatomic, weak) UIButton *currentVideoBtn;
/*** 滚动视图 */
@property (nonatomic, weak) UIScrollView *scrollView;
/*** 滚动指示器 */
@property (nonatomic, weak) UIScrollView *sliderView;
/***  切换按钮 */
@property (nonatomic, strong) NSMutableArray *tabBtnArr;
/***  简介图片 */
@property (nonatomic, weak) UIImageView *briefView;
/***  视频名称 */
@property (nonatomic, weak) UILabel *titleLabel;
/***  视频描述 */
@property (nonatomic, weak) UILabel *describeLabel;
/***  视频内容介绍 */
@property (nonatomic, weak) UILabel *detailLabel;
@end

@implementation LXMovieShowViewController

- (NSMutableArray *)tabBtnArr
{
    if (_tabBtnArr == nil) {
        _tabBtnArr = [NSMutableArray array];
    }
    return _tabBtnArr;
}

- (void)setVideo:(LXVideo *)video
{
    if (video != nil) {
        _video = video;
        //监听视频的播放量
        [self.video addObserver:self forKeyPath:@"viewcounts" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:nil];
    }
}

#pragma mark - 监听视频的播放量
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context
{
    if ([keyPath isEqualToString:@"viewcounts"]) {
        NSString *describe = [NSString stringWithFormat:@"已完结\n%ld次播放 %ld次评论",self.video.viewcounts,self.video.videocomments];
        self.describeLabel.text = describe;
    }
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self initNavBar];
    
    [self initTopView];
    
    [self initBottomView];
}

#pragma mark 自定义导航条
- (void)initNavBar
{
    LXNavBarView *navBarView = [[LXNavBarView alloc]init];
    navBarView.frame = CGRectMake(0, 20, self.view.frame.size.width, 40);
    navBarView.myDelegate = self;
    [self.view addSubview:navBarView];
}

#pragma mark - LXNavBarView代理方法
- (void)popCurrentViewController:(LXNavBarView *)navBarView
{
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark 初始化顶部视图
- (void)initTopView
{
    UIView *topView = [[UIView alloc]init];
    topView.backgroundColor = [UIColor whiteColor];
    CGFloat topViewX = 0;
    CGFloat topViewY = 60;
    CGFloat topViewW = self.view.frame.size.width;
    CGFloat topViewH = 200;
    topView.frame = CGRectMake(topViewX, topViewY, topViewW, topViewH);
    [self.view addSubview:topView];
    self.topView = topView;
    
    UIImageView *imageView = [[UIImageView alloc]init];
    CGFloat imageViewX = LXMovieShowViewControllerMargin;
    CGFloat imageViewY = LXMovieShowViewControllerMargin;
    CGFloat imageViewW = 100;
    CGFloat imageViewH = 120;
    imageView.frame = CGRectMake(imageViewX, imageViewY, imageViewW, imageViewH);
    [topView addSubview:imageView];
    
    self.briefView = imageView;
    [self.briefView sd_setImageWithURL:[NSURL URLWithString:self.video.thumb]];
    

    UILabel *titleLabel = [[UILabel alloc]init];
    titleLabel.font = [UIFont boldSystemFontOfSize:14];
    titleLabel.numberOfLines = 0;
    CGFloat titleLabelX = CGRectGetMaxX(imageView.frame) + LXMovieShowViewControllerMargin;
    CGFloat titleLabelY = imageViewY;
    CGFloat titleLabelW = topViewW - imageViewW - 3 * LXMovieShowViewControllerMargin;
    CGFloat titleLabelH = 35;
    titleLabel.frame = CGRectMake(titleLabelX, titleLabelY, titleLabelW, titleLabelH);
    [topView addSubview:titleLabel];
    
    self.titleLabel = titleLabel;
    self.titleLabel.text = self.video.fname;
    
    UILabel *describeLabel = [[UILabel alloc]init];
    describeLabel.numberOfLines = 2;
    describeLabel.font = [UIFont systemFontOfSize:14];
    CGFloat describeLabelX = titleLabelX;
    CGFloat describeLabelY = CGRectGetMaxY(titleLabel.frame) + 0.5 * LXMovieShowViewControllerMargin;
    CGFloat describeLabelW = titleLabelW;
    CGFloat describeLabelH = 40;
    describeLabel.frame = CGRectMake(describeLabelX, describeLabelY, describeLabelW, describeLabelH);
    [topView addSubview:describeLabel];
    
    self.describeLabel = describeLabel;
    NSString *describe = [NSString stringWithFormat:@"已完结\n%ld次播放 %ld次评论",self.video.viewcounts,self.video.videocomments];
    self.describeLabel.text = describe;
    
    UIButton *playBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    playBtn.titleLabel.font = [UIFont boldSystemFontOfSize:15];
    [playBtn setTitle:@"播放" forState:UIControlStateNormal];
    [playBtn setBackgroundImage:[UIImage resizeWithName:@"button-blue"] forState:UIControlStateNormal];
    [playBtn addTarget:self action:@selector(moviePlay) forControlEvents:UIControlEventTouchUpInside];
    CGFloat playBtnX = titleLabelX;
    CGFloat playBtnY = CGRectGetMaxY(describeLabel.frame) + 0.5 * LXMovieShowViewControllerMargin;
    CGFloat playBtnW = 150;
    CGFloat playBtnH = 30;
    playBtn.frame = CGRectMake(playBtnX, playBtnY, playBtnW, playBtnH);
    [topView addSubview:playBtn];
    
    UILabel *detailLabel = [[UILabel alloc]init];
    self.detailLabel = detailLabel;
    self.detailLabel.text = self.video._description;
    detailLabel.font = [UIFont systemFontOfSize:13];
    detailLabel.numberOfLines = 0;
    CGFloat detailLabelX = imageViewX;
    CGFloat detailLabelY = CGRectGetMaxY(imageView.frame) + LXMovieShowViewControllerMargin;
    CGFloat detailLabelW = topViewW - 2 * LXMovieShowViewControllerMargin;
    //计算本文高度
    CGSize detailLabelSize = [detailLabel.text boundingRectWithSize:CGSizeMake(detailLabelW, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:detailLabel.font} context:nil].size;
    CGFloat detailLabelH = detailLabelSize.height;
    detailLabel.frame = CGRectMake(detailLabelX, detailLabelY, detailLabelW, detailLabelH);

    [topView addSubview:detailLabel];
    
}

/**
 *  视频播放
 */
- (void)moviePlay
{
    MoviePlayerViewController *vc = [[MoviePlayerViewController alloc]init];
    vc.video = self.video;

    [self.navigationController presentViewController:vc animated:YES completion:nil];
}


#pragma mark 初始化initTabView
- (void)initBottomView
{
    UIView *bottomView = [[UIView alloc]init];
    CGFloat bottomViewX = 0;
    CGFloat bottomViewY = CGRectGetMaxY(self.topView.frame) - LXMovieShowViewControllerHideHeight;
    CGFloat bottomViewW = self.view.frame.size.width;
    CGFloat bottomViewH = self.view.frame.size.height - self.topView.frame.size.height;
    bottomView.frame = CGRectMake(bottomViewX, bottomViewY, bottomViewW, bottomViewH);
    [self.view addSubview:bottomView];
    self.bottomView = bottomView;
    
    //切换按钮视图
    UIView *tabView = [[UIView alloc]init];
    tabView.backgroundColor = [UIColor whiteColor];
    CGFloat tabViewX = 0;
    CGFloat tabViewY = 0;
    CGFloat tabViewW = bottomViewW;
    CGFloat tabViewH = 50;
    tabView.frame = CGRectMake(tabViewX, tabViewY, tabViewW, tabViewH);
    [bottomView addSubview:tabView];
    
    //添加切换按钮
    NSArray *tabTitleArr = @[@"推荐",@"评论"];
    for (int i=0; i<2; i++) {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [btn setTitleColor:LXColor(41, 188, 208) forState:UIControlStateSelected];
        [btn addTarget:self action:@selector(tabClick:) forControlEvents:UIControlEventTouchUpInside];
        [btn setTitle:tabTitleArr[i] forState:UIControlStateNormal];
        btn.tag = i;
        btn.titleLabel.font = [UIFont systemFontOfSize:15];
        CGFloat btnW = tabViewW / tabTitleArr.count;
        CGFloat btnX = i * btnW;
        CGFloat btnY = 0;
        CGFloat btnH = tabViewH;
        btn.frame = CGRectMake(btnX, btnY, btnW, btnH);
        [tabView addSubview:btn];
        [self.tabBtnArr addObject:btn];
        if (i == 0) {
            [self tabClick:btn];
        }
    }
    
    //添加滚动指示器
    UIScrollView *sliderView = [[UIScrollView alloc]init];
    sliderView.frame = CGRectMake(0, tabViewH - 3, tabViewW, 3);
    self.sliderView = sliderView;
    [tabView addSubview:sliderView];
    
    //添加红色指示器
    UIView *view = [[UIView alloc]init];
    view.backgroundColor = LXColor(41, 188, 208);
    
    CGFloat viewW = 25;
    CGFloat viewH = sliderView.frame.size.height;
    CGFloat viewX = tabViewW / 4 ;
    CGFloat viewY = 0;
    view.frame = CGRectMake(viewX, viewY, viewW, viewH);
    
    CGPoint point = view.center;
    point.x = tabViewW / 4;
    view.center = point;
    
    [sliderView addSubview:view];
    
    //添加折叠按钮
    UIButton *hideBtn = [UIButton buttonWithType:UIButtonTypeContactAdd];
    hideBtn.tag = 0;
    [hideBtn addTarget:self action:@selector(hide:) forControlEvents:UIControlEventTouchUpInside];
    CGFloat hideBtnW = 30;
    CGFloat hideBtnH = 30;
    CGFloat hideBtnX = bottomViewW - hideBtnW;
    CGFloat hideBtnY = 0;
    hideBtn.frame = CGRectMake(hideBtnX, hideBtnY, hideBtnW, hideBtnH);
    [bottomView addSubview:hideBtn];
    
    //切换滚动视图
    UIScrollView *scrollView = [[UIScrollView alloc]init];
    scrollView.backgroundColor = MainBackgroundColor;
    scrollView.pagingEnabled = YES;
    scrollView.delegate = self;
    CGFloat scrollViewX = 0;
    CGFloat scrollViewY = tabViewH;
    CGFloat scrollViewW = tabViewW;
    CGFloat scrollViewH = bottomViewH - tabViewH;
    scrollView.frame = CGRectMake(scrollViewX, scrollViewY, scrollViewW, scrollViewH);
    scrollView.contentSize = CGSizeMake(tabTitleArr.count * scrollViewW, scrollViewH);
    [bottomView addSubview:scrollView];
    self.scrollView = scrollView;
    
    // 选集视图
    UIView *selectedView = [[UIView alloc]init];
    CGFloat selectedViewX = 0;
    CGFloat selectedViewY = 0;
    CGFloat selectedViewW = scrollViewW;
    CGFloat selectedViewH = scrollViewH;
    selectedView.frame = CGRectMake(selectedViewX, selectedViewY, selectedViewW, selectedViewH);
    [scrollView addSubview:selectedView];
    
    int column = 4; //每列放四个按钮
    int margin = LXMovieShowViewControllerMargin;
    CGFloat btnH = 30;
    CGFloat btnW = (selectedViewW - 5 * margin) / 4;
    for (int i=0; i<13; i++) {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [btn setBackgroundImage:[UIImage resizeWithName:@"button"] forState:UIControlStateNormal];
        [btn setBackgroundImage:[UIImage resizeWithName:@"button-blue"] forState:UIControlStateSelected];
        [btn setTitle:[NSString stringWithFormat:@"第%d话",i+1] forState:UIControlStateNormal];
        [btn addTarget:self action:@selector(videoPlay:) forControlEvents:UIControlEventTouchUpInside];
        btn.titleLabel.font = [UIFont systemFontOfSize:15];
        int row = i / column; //行数
        int col = i % column; // 列数
        CGFloat btnX = margin + (margin + btnW) * col;
        CGFloat btnY = margin + (margin + btnH) * row;
        btn.frame = CGRectMake(btnX, btnY, btnW, btnH);
        [selectedView addSubview:btn];
    }
   
}

/**
 *  隐藏或展现顶部内容
 */
- (void)hide:(UIButton *)btn
{
    CGRect bottomViewFrame = self.bottomView.frame;
    if (btn.tag == 0) {
        btn.tag = 1;
        bottomViewFrame.origin.y += LXMovieShowViewControllerHideHeight;
    }else{
        btn.tag = 0;
        bottomViewFrame.origin.y -= LXMovieShowViewControllerHideHeight;
    }
    
    [UIView animateWithDuration:0.3 animations:^{
        self.bottomView.frame = bottomViewFrame;
    }];
}

#pragma mark 选项卡切换变化视图
- (void)tabClick:(UIButton *)btn
{
    if (btn.selected == YES) {
        return;
    }
    self.currentBtn.selected = NO;
    btn.selected = YES;
    self.currentBtn = btn;
    
    [self.scrollView setContentOffset:CGPointMake(btn.tag * self.scrollView.frame.size.width, 0) animated:YES];
}

#pragma mark 选集切换
- (void)videoPlay:(UIButton *)btn
{
    self.currentVideoBtn.selected = NO;
    btn.selected = YES;
    self.currentVideoBtn = btn;
    
    MoviePlayerViewController *vc = [[MoviePlayerViewController alloc]init];
    [self.navigationController presentViewController:vc animated:YES completion:nil];
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    if ([scrollView isKindOfClass:self.scrollView.class]) {
        int num = self.scrollView.contentOffset.x / self.view.frame.size.width;
        if (self.currentBtn.tag != num) {
            [self tabClick:self.tabBtnArr[num]];
        }
    }
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if ([scrollView isKindOfClass:self.scrollView.class]) {
        CGPoint point = self.scrollView.contentOffset;
        point.x = -point.x / 2;
        self.sliderView.contentOffset = point;
    }
}

#pragma mark - 控制器销毁
- (void)dealloc
{
    [self.video removeObserver:self forKeyPath:@"viewcounts"];
}
@end
